package controller;

import controller.packing.*;
import javafx.collections.*;
import javafx.event.*;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.*;
import java.net.URL;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Date;
import java.util.regex.Pattern;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;

/**
 * 挂号界面的controller
 */
public class Register implements Initializable
{
    /**
     * 数据库URL
     */
    static final String DBURL = "jdbc:mysql://localhost:3306/hospital_data?useSSL=false&serverTimezone=UTC";
    /**
     * 数据库用户名
     */
    static final String USER = "root";
    /**
     * 数据库密码
     */
    static final String PASS = "1999";
    /**
     * 数据库连接对象
     */
    static Connection conn = null;
    /**
     * 数据库语句对象
     */
    static Statement stmt = null;
    /**
     *
     */
    private boolean flag_of_changedbysel = false;

    private boolean flag_of_changedbyreg = false;

    private boolean flag_of_changedbyunreg = false;

    boolean flag_off_cut = false;

    String sel_off_num,sel_doc_num,sel_reg_num;
    /**
     * 科室名称列表
     */
    ObservableList<String> office = FXCollections.observableArrayList();
    /**
     *
     */
    ObservableList<String> cut_office = FXCollections.observableArrayList();
    /**
     * 医生编号
     */
    ObservableList<String> ob_doc = FXCollections.observableArrayList();
    /**
     * 患病名称
     */
    ObservableList<String> ob_regname = FXCollections.observableArrayList();
    /**
     * 病人姓名
     */
    ObservableList<RegList> ob_patreg = FXCollections.observableArrayList();
    ObservableList<RegList> ob_unreg = FXCollections.observableArrayList();
    /**
     * 科室编号列表，
     */
    Vector<String> office_list,cut_of_list,doc_list,reg_list;
    /**
     * 确认、清楚、退出按钮对象
     */
    @FXML
    private Button btn_ok,btn_clear,btn_exit;
    /**
     * 科室列表、医生列表，用户类型，
     */
    @FXML
    private ListView<String> list_office,list_doc,list_regtype,list_regname;
    /**
     * 菜单对象
     */
    @FXML
    private ContextMenu context_office,context_doc,context_regtype,context_regname;
    /**
     * 文本框对象
     */
    @FXML
    private TextField text_office,text_doc,text_regtype,text_regname,text_cost,
            text_pay,text_charge,text_regnum;
    /**
     * 挂号表
     */
    @FXML
    private TableView<RegList> table_patreg;
    /**
     * 挂号表属性
     */
    @FXML
    private TableColumn<?, ?>col_regnum,col_regname,col_docnum,col_docname,
            col_regcount,col_regcost,col_unreg,col_regtime;
    /**
     * 挂号和退号标签对象
     */
    @FXML
    private Tab tab_reg,tab_unreg;

    /**
     * 初始化：连接数据库，加载用户信息，获取账号
     * @param location
     * @param resources
     */
    @Override
    public void initialize(URL location, ResourceBundle resources)
    {
        //使用格式工厂进行属性绑定
        col_regnum.setCellValueFactory(new PropertyValueFactory<>("regNum"));
        col_regname.setCellValueFactory(new PropertyValueFactory<>("regName"));
        col_docnum.setCellValueFactory(new PropertyValueFactory<>("docNum"));
        col_docname.setCellValueFactory(new PropertyValueFactory<>("docName"));
        //使用格式工厂进行属性绑定
        col_regcount.setCellValueFactory(new PropertyValueFactory<>("regCount"));
        col_regcost.setCellValueFactory(new PropertyValueFactory<>("regCost"));
        col_unreg.setCellValueFactory(new PropertyValueFactory<>("unReg"));
        col_regtime.setCellValueFactory(new PropertyValueFactory<>("regTime"));
        table_patreg.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);

        try
        {
            office_list = new Vector<>();
            cut_of_list = new Vector<>();
            doc_list = new Vector<>();
            reg_list = new Vector<>();
            conn = DriverManager.getConnection(DBURL,USER,PASS);
            stmt = conn.createStatement();
            //查询科室信息
            String sql = "select office_num,office_name from office";
            ResultSet rs = stmt.executeQuery(sql);
            String office_name,office_num;
            while(rs.next())
            {
                office_num = rs.getString("office_num");
                office_name = rs.getString("office_name");
                office.add(office_name);
                office_list.add(office_num);
            }
            rs.close();
            stmt.close();
            conn.close();
        }catch(SQLException se){
            se.printStackTrace();
        }
        //设置科室信息
        list_office.setItems(office);
        /**
         * 给医生选项添加监听函数，监听输入框的改变
         */
        list_doc.getSelectionModel().selectedItemProperty().addListener
                (
                        (ObservableValue<? extends String> observable,
                         String oldValue, String newValue) ->
                        {
                            int index = list_doc.getSelectionModel().getSelectedIndex();
                            if(index>-1)
                            {
                                sel_doc_num = doc_list.elementAt(index);
                            }

                            text_doc.setText(newValue);
                            context_doc.hide();
                        }
                );
        /**
         * 给号别种类选项卡添加监听函数，监听输入框的改变
         */
        list_regtype.getSelectionModel().selectedItemProperty().addListener
                (
                        (ObservableValue<? extends String> observable,
                         String oldValue, String newValue) ->
                        {
                            text_regtype.setText(newValue);
                            context_regtype.hide();
                        }
                );
        /**
         * 给好种名称添加监听函数，监听输入框的改变
         */
        list_regname.getSelectionModel().selectedItemProperty().addListener
                (
                        (ObservableValue<? extends String> observable,
                         String oldValue, String newValue) ->
                        {
                            int index = list_regname.getSelectionModel().getSelectedIndex();
                            if(index>-1)
                            {
                                sel_reg_num = reg_list.elementAt(index);
                            }
                            text_regname.setText(newValue);
                            context_regname.hide();
                        }
                );
        /**
         * 给科室添加监听函数，监听输入框的改变
         */
        text_office.textProperty().addListener(new ChangeListener<String>()
                                               {
                                                   @Override
                                                   public void changed(ObservableValue<? extends String> observable,
                                                                       String oldValue, String newValue)
                                                   {
                                                       if(flag_of_changedbyreg==true)
                                                       {
                                                           flag_of_changedbyreg = false;
                                                           return;
                                                       }
                                                       if(flag_of_changedbysel==false)
                                                       {
                                                           context_office.show(Main.primaryStage,
                                                                   Main.primaryStage.getX()+128.0+10,
                                                                   Main.primaryStage.getY()+33.0+130);
                                                       }
                                                       else
                                                           flag_of_changedbysel = false;
                                                       Pattern pattern = Pattern.compile("[0-9a-zA-Z]*");
                                                       if(newValue==null||newValue.isEmpty())
                                                       {
                                                           list_office.setItems(office);
                                                           flag_off_cut = false;
                                                       }
                                                       else if(pattern.matcher(newValue).matches())//非汉字
                                                       {
                                                           try
                                                           {
                                                               conn = DriverManager.getConnection(DBURL,USER,PASS);
                                                               stmt = conn.createStatement();
                                                               String sql = "select office_num,office_name from office "
                                                                       + "where office_pinyin like '"+newValue+"%'";
                                                               ResultSet rs = stmt.executeQuery(sql);
                                                               cut_office.clear();
                                                               cut_of_list.clear();
                                                               String office_name,office_num;
                                                               while(rs.next())
                                                               {
                                                                   office_num = rs.getString("office_num");
                                                                   office_name = rs.getString("office_name");
                                                                   cut_office.add(office_name);
                                                                   cut_of_list.add(office_num);
                                                               }
                                                               list_office.setItems(cut_office);
                                                               flag_off_cut = true;
                                                               rs.close();
                                                               stmt.close();
                                                               conn.close();
                                                           }catch(SQLException se){
                                                               // 处理 JDBC 错误
                                                               se.printStackTrace();
                                                           }
                                                       }
                                                   }
                                               }
        );
    }

    /**
     * 切换Tab页面触发函数
     * @param event
     */
    @FXML
    public void changeTab(Event event)
    {
        boolean flag_unreg=false,expert=false;
        String regnum,regname,docnum,docname,regcount,regcost,unreg,regtime;
        try
        {
            //连接数据库
            conn = DriverManager.getConnection(DBURL,USER,PASS);
            stmt = conn.createStatement();
            String sql;
            ResultSet rs=null;
            //查询挂号信息
            sql = "select count(*) as regcount from register " +
                    "where pat_num='"+Main.accountNum +"'";
            rs = stmt.executeQuery(sql);
            //对推号列表进行更新
            if(rs.next())
            {
                int count = rs.getInt("regcount");
                int col_count = ob_patreg.size();
     			/*System.out.println("count="+count);
     			System.out.println("colcount="+col_count);*/
                if(count!=col_count || flag_of_changedbyunreg)
                {
                    ob_patreg.clear();
                    sql = "select reg1.register_num,reg2.reg_name,reg2.expert,reg1.doc_num,"
                            + "doc.doc_name,reg1.pat_count,reg1.unreg,reg1.reg_cost,"
                            + "reg1.reg_datetime from register reg1,doctor doc,"
                            + "registration reg2 where reg1.pat_num='"+Main.accountNum +"' "
                            + "and doc.doc_num=reg1.doc_num and reg2.reg_num=reg1.registration_num";
                    rs = stmt.executeQuery(sql);
                    while(rs.next())
                    {
                        //更新推号列表
                        regnum = rs.getString("reg1.register_num");
                        regname = rs.getString("reg2.reg_name");
                        expert = rs.getBoolean("reg2.expert");
                        regname = regname+"（"+(expert?"专家":"普通")+"号）";
                        docnum = rs.getString("reg1.doc_num");
                        docname = rs.getString("doc.doc_name");
                        regcount = rs.getString("reg1.pat_count");
                        regcost = rs.getString("reg1.reg_cost");
                        flag_unreg = rs.getBoolean("reg1.unreg");
                        unreg = flag_unreg?"是":"否";
                        regtime = rs.getString("reg1.reg_datetime");
                        ob_patreg.add(new RegList(regnum,regname,docnum,docname,regcount,
                                regcost,unreg,regtime));
                    }
                    table_patreg.setItems(ob_patreg);
                    flag_of_changedbyunreg = false;
                }
            }
            rs.close();
            //关闭数据库
            stmt.close();
            conn.close();
        }catch(SQLException se){
            se.printStackTrace();
        }
    }

    /**
     * 清空按钮的功能函数
     * @param event
     */
    @FXML
    public void clearClicked(ActionEvent event)
    {
        table_patreg.getSelectionModel().clearSelection();
    }

    /**
     * 退号按钮功能函数
     * @param event
     */
    @FXML
    public void unregClicked(ActionEvent event)
    {

        ob_unreg = table_patreg.getSelectionModel().getSelectedItems();
        RegList patreg;
        //查询当前挂号列表
        for(int i=0;i<ob_unreg.size();++i)
        {
            patreg = ob_unreg.get(i);
            String str_unreg = patreg.getUnReg();
            boolean flag_unreg = str_unreg.equals("是")?true:false;
            if(flag_unreg)
            {
                JOptionPane.showMessageDialog(new JFrame().getContentPane(),
                        "不可重复退号", "警告", JOptionPane.WARNING_MESSAGE);
                continue;
            }
            flag_of_changedbyunreg = true;
            try
            {
                conn = DriverManager.getConnection(DBURL,USER,PASS);
                stmt = conn.createStatement();
                //更新退号病人的信息
                String sql = "update register set unreg=1 where register_num"
                        + "= '"+patreg.getRegNum()+"'";
                stmt.executeUpdate(sql);

                sql = "update register set pat_count=pat_count-1 where " +
                        "registration_num in ( select reg3.registration_num from " +
                        "(select registration_num from register " +
                        "where register_num='"+patreg.getRegNum()+"') reg3)";
                stmt.executeUpdate(sql);
                //关闭数据库
                stmt.close();
                conn.close();
            }catch(SQLException se){
                se.printStackTrace();
            }
        }
        changeTab(new Event(null));
    }

    /**
     * 显示科室列表
     * @param event
     */
    @FXML
    public void officeListClicked(Event event)
    {
        String newoffice = list_office.getSelectionModel().getSelectedItem();
        int index = list_office.getSelectionModel().getSelectedIndex();
        flag_of_changedbysel = true;
        if(index>-1)
        {
            sel_off_num = flag_off_cut ? cut_of_list.elementAt(index) :
                    office_list.elementAt(index);
        }
        text_office.setText(newoffice);
        context_office.hide();
    }

    /**
     * 清楚科室信息
     * @param event
     */
    @FXML
    public void clearOfficeClicked(Event event)
    {
        text_doc.clear();
        text_regtype.clear();
        text_regname.clear();
        text_cost.clear();
        text_pay.clear();
        text_charge.clear();
        text_regnum.clear();
        context_office.show(Main.primaryStage,
                Main.primaryStage.getX()+128.0+10,
                Main.primaryStage.getY()+33.0+130);
    }

    /**
     * 选择医生
     * @param event
     */
    @FXML
    public void chooseDoctorClicked(Event event)
    {
        text_regtype.clear();
        text_regname.clear();
        text_cost.clear();
        text_pay.clear();
        text_charge.clear();
        text_regnum.clear();
        String office = text_office.getText();
        if(office==null||office.isEmpty())
        {
            context_doc.hide();
            JOptionPane.showMessageDialog(new JFrame().getContentPane(),
                    "请先选择科室", "警告", JOptionPane.WARNING_MESSAGE);
        }
        else
        {
            ob_doc.clear();
            try
            {
                doc_list.clear();
                conn = DriverManager.getConnection(DBURL,USER,PASS);
                stmt = conn.createStatement();
                String sql = "select doc_num,doc_name from doctor where office_num ="
                        + "'"+sel_off_num+"'";
                ResultSet rs = stmt.executeQuery(sql);
                String doc_name,doc_num;
                while(rs.next())
                {
                    doc_num = rs.getString("doc_num");
                    doc_name = rs.getString("doc_name");
                    ob_doc.add(doc_name);
                    doc_list.add(doc_num);
                }
                rs.close();
                stmt.close();
                conn.close();
            }catch(SQLException se){
                se.printStackTrace();
            }
            list_doc.setItems(ob_doc);
            context_doc.show(Main.primaryStage,
                    Main.primaryStage.getX()+385.0+10,
                    Main.primaryStage.getY()+33.0+130);
        }
    }

    /**
     * 选择号别种类
     * @param event
     */
    @FXML
    public void chooseRegTypeClicked(Event event)
    {
        text_regname.clear();
        text_cost.clear();
        text_pay.clear();
        text_charge.clear();
        text_regnum.clear();
        String doctor = text_doc.getText();
        if(doctor==null||doctor.isEmpty())
        {
            context_doc.hide();
            JOptionPane.showMessageDialog(new JFrame().getContentPane(),
                    "请先选择医生", "警告", JOptionPane.WARNING_MESSAGE);
        }
        else
        {
            list_regtype.getItems().clear();
            try
            {
                conn = DriverManager.getConnection(DBURL,USER,PASS);
                stmt = conn.createStatement();
                String sql = "select expert from doctor where "
                        + "doc_num = '"+sel_doc_num+"'";
                ResultSet rs = stmt.executeQuery(sql);
                if(rs.next())
                {
                    boolean expert = rs.getBoolean("expert");
                    if(expert)
                    {
                        list_regtype.getItems().add("普通号");
                        list_regtype.getItems().add("专家号");
                    }
                    else
                    {
                        list_regtype.getItems().add("普通号");
                    }
                    context_regtype.show(Main.primaryStage,
                            Main.primaryStage.getX()+128.0+10,
                            Main.primaryStage.getY()+80.0+130);
                }
                else
                {
                    JOptionPane.showMessageDialog(new JFrame().getContentPane(),
                            "该医生不可挂号", "警告", JOptionPane.WARNING_MESSAGE);
                    return;
                }
                //关闭数据库
                rs.close();
                stmt.close();
                conn.close();
            }catch(SQLException se){
                se.printStackTrace();
            }
        }
    }

    /**
     * 选择号种名称
     * @param event
     */
    @FXML
    public void chooseRegNameClicked(Event event)
    {
        text_cost.clear();
        text_pay.clear();
        text_charge.clear();
        text_regnum.clear();
        String office = text_office.getText();
        String expertstr = text_regtype.getText();
        if(office==null||office.isEmpty()
                ||expertstr==null||expertstr.isEmpty())
        {
            context_regname.hide();
            JOptionPane.showMessageDialog(new JFrame().getContentPane(),
                    "请先选择号种类别", "警告", JOptionPane.WARNING_MESSAGE);
            return;
        }
        int expert = expertstr.equals("专家号") ? 1 :0;
        ob_regname.clear();
        reg_list.clear();
        try
        {
            conn = DriverManager.getConnection(DBURL,USER,PASS);
            stmt = conn.createStatement();
            //查询号种信息
            String sql = "select reg_num,reg_name from registration where "
                    + "office_num = '"+sel_off_num+"' and "
                    + "expert = '"+expert+"'";
            ResultSet rs = stmt.executeQuery(sql);
            String reg_name,reg_num;
            while(rs.next())
            {
                reg_num = rs.getString("reg_num");
                reg_name = rs.getString("reg_name");
                reg_list.add(reg_num);
                ob_regname.add(reg_name);
            }
            rs.close();
            stmt.close();
            conn.close();
            if(ob_regname.isEmpty())
            {
                JOptionPane.showMessageDialog(new JFrame().getContentPane(),
                        "没有符合条件的号", "警告", JOptionPane.WARNING_MESSAGE);
            }
            else
            {
                list_regname.setItems(ob_regname);
                context_regname.show(Main.primaryStage,
                        Main.primaryStage.getX()+385.0+10,
                        Main.primaryStage.getY()+80.0+130);
            }
        }catch(SQLException se){
            se.printStackTrace();
        }
    }

    /**
     * 获得号种编号，检索预存金额进行缴费
     * @param event
     */
    @FXML
    public void submitCostClicked(Event event)
    {
        text_pay.clear();
        text_charge.clear();
        text_regnum.clear();
        String regname = text_regname.getText();
        if(regname==null||regname.isEmpty())
        {
            JOptionPane.showMessageDialog(new JFrame().getContentPane(),
                    "请先选择号种类别", "警告", JOptionPane.WARNING_MESSAGE);
        }
        else
        {
            try
            {
                double reg_cost;
                conn = DriverManager.getConnection(DBURL,USER,PASS);
                stmt = conn.createStatement();
                String sql = "select reg_cost from registration where "
                        + "reg_num = '"+sel_reg_num+"'";
                ResultSet rs = stmt.executeQuery(sql);
                if(rs.next())
                {
                    reg_cost = rs.getDouble("reg_cost");
                    text_cost.setText(Double.toString(reg_cost));
                }
                else
                {
                    JOptionPane.showMessageDialog(new JFrame().getContentPane(),
                            "没有符合条件的号", "警告", JOptionPane.WARNING_MESSAGE);
                    return;
                }
                sql = "select prestore_amount from patient where "
                        + "pat_num = '"+Main.accountNum +"'";
                rs = stmt.executeQuery(sql);
                if(rs.next())
                {
                    double prestore = rs.getDouble("prestore_amount");
                    if(prestore>=reg_cost)
                    {
                        text_pay.setText(Double.toString(prestore));
                        text_pay.setEditable(false);
                        text_charge.setText(Double.toString(prestore-reg_cost));
                    }
                    else
                    {
                        text_pay.setPromptText("请缴纳至少"+reg_cost+"元");
                        text_pay.setEditable(true);
                    }
                }
                rs.close();
                stmt.close();
                conn.close();
            }catch(SQLException se){
                se.printStackTrace();
            }
        }
    }

    /**
     * 清空缴费文本框
     * @param event
     */
    @FXML
    public void clearCostClicked(Event event)
    {
        text_charge.clear();
        text_regnum.clear();
        text_pay.setPromptText("");
    }

    /**
     * 判断预存金额是否足够，不足则需要手动缴费
     * @param event
     */
    @FXML
    public void chargeClicked(Event event)
    {
        text_regnum.clear();
        String pays = text_pay.getText();
        String costs = text_cost.getText();
        if(pays==null||pays.isEmpty())
        {
            JOptionPane.showMessageDialog(new JFrame().getContentPane(),
                    "请先输入交款金额！", "警告", JOptionPane.WARNING_MESSAGE);
        }
        else
        {
            double pay = Double.parseDouble(pays);
            double cost = Double.parseDouble(costs);
            if(pay>=cost)
            {
                text_charge.setText(Double.toString(pay-cost));
            }
            else
            {
                JOptionPane.showMessageDialog(new JFrame().getContentPane(),
                        "交款金额不足！", "警告", JOptionPane.WARNING_MESSAGE);
            }
        }
    }

    /**
     * 挂号按钮的功能除法函数
     * @param event
     */
    @FXML
    public void confirmBtnClicked(ActionEvent event)
    {
        int max_pat = 0,regcount=0;
        String regcost = text_cost.getText();
        int registernum=0;
        String charge_str = text_charge.getText();
        if(charge_str==null||charge_str.isEmpty())
        {
            JOptionPane.showMessageDialog(new JFrame().getContentPane(),
                    "请先填写信息！", "警告", JOptionPane.WARNING_MESSAGE);
            return;
        }
        try
        {
            String currtime=null;
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            conn = DriverManager.getConnection(DBURL,USER,PASS);
            stmt = conn.createStatement();

            String sql = "select max_patient from registration where "
                    + "reg_num='"+sel_reg_num+"'";
            ResultSet rs = stmt.executeQuery(sql);
            if(rs.next())
            {
                max_pat = rs.getInt("max_patient");
            }

            sql = "select current_date as currtime";
            rs = stmt.executeQuery(sql);
            if(rs.next())
            {
                currtime = rs.getString("currtime");
                currtime += " 00:00:00";
            }

            sql = "select count(*) as regcount from register "
                    + "where registration_num ='"+sel_reg_num+"' and "
                    + "reg_datetime>='"+currtime+"' and unreg=0";
            rs = stmt.executeQuery(sql);
            if(rs.next())
            {
                regcount = rs.getInt("regcount");
            }
            if(regcount>=max_pat)
            {
                JOptionPane.showMessageDialog(new JFrame().getContentPane(),
                        "该号种已经挂满！", "警告", JOptionPane.WARNING_MESSAGE);
            }
            else
            {
                sql = "select count(*) as regtotal from register";
                rs = stmt.executeQuery(sql);
                if(rs.next())
                {
                    registernum = rs.getInt("regtotal");
                    registernum++;
                }
                text_regnum.setText(Integer.toString(registernum));

                sql = "insert into register (register_num,registration_num,"
                        + "doc_num,pat_num,pat_count,unreg,reg_datetime,reg_cost) "
                        + "values ('"+registernum+"','"+sel_reg_num+"','"+sel_doc_num+"',"
                        + "'"+Main.accountNum +"','"+(1+regcount)+"','"+0+"','"+df.format(new Date())+"'"
                        + ",'"+regcost+"')";
                stmt.executeUpdate(sql);

                double charge;
                charge = Double.parseDouble(text_charge.getText());
                if(text_pay.isEditable())
                {
                    sql = "update patient set prestore_amount = "
                            + ""+charge+"+prestore_amount where "
                            + "pat_num= '"+Main.accountNum +"'";
                }
                else
                {
                    sql = "update patient set prestore_amount = "
                            + ""+charge+"where pat_num= '"+Main.accountNum +"'";
                }
                stmt.executeUpdate(sql);

                sql = "update register set pat_count= '"+(1+regcount)+"' where "
                        + "registration_num='"+sel_reg_num+"' and reg_datetime>="
                        + "'"+currtime+"'";
                stmt.executeUpdate(sql);

                flag_of_changedbyreg = true;
                text_office.clear();
                text_doc.clear();
                text_regtype.clear();
                text_regname.clear();
                text_cost.clear();
                text_pay.clear();
                text_charge.clear();
                text_regnum.clear();
                JOptionPane.showMessageDialog(new JFrame().getContentPane(),
                        "挂号成功！\n挂号编号为"+registernum, "提示", JOptionPane.INFORMATION_MESSAGE);
            }
            rs.close();
            stmt.close();
            conn.close();
        }catch(SQLException se){
            se.printStackTrace();
        }
    }

    /**
     * 退出按钮的功能触发函数
     * @param event
     */
    @FXML
    public void exitBtnClicked(ActionEvent event)
    {
        Event.fireEvent(Main.getPrimaryStage(),
                new WindowEvent(Main.getPrimaryStage(), WindowEvent.WINDOW_CLOSE_REQUEST ));
    }

    /**
     * 清空按钮的功能除法函数
     * @param event
     */
    @FXML
    public void clearBtnClicked(ActionEvent event)
    {
        flag_of_changedbyreg = true;
        text_office.clear();
        text_doc.clear();
        text_regtype.clear();
        text_regname.clear();
        text_cost.clear();
        text_pay.clear();
        text_charge.clear();
        text_regnum.clear();
    }
    @FXML
    /**
     * 退出登录按钮的功能触发函数
     */
    public void logoutBtnClicked(ActionEvent event)
    {
        Main.setLogin();
    }
}
